package com.mcc.app.materials.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.app.materials.mapper.SinterMaterialsMapper;
import com.mcc.app.materials.domain.SinterMaterials;
import com.mcc.app.materials.service.ISinterMaterialsService;

/**
 * 物料管理Service业务层处理
 *
 * @author gxy
 * @date 2025-04-03
 */
@Service
public class SinterMaterialsServiceImpl implements ISinterMaterialsService {

    @Autowired
    private SinterMaterialsMapper sinterMaterialsMapper;

    /**
     * 查询物料管理
     *
     * @param id 物料管理ID
     * @return 物料管理
     */
    @Override
    public SinterMaterials selectSinterMaterialsById(Long id) {
        return sinterMaterialsMapper.selectById(id);
    }

    /**
     * 查询物料管理列表
     *
     * @param sinterMaterials 物料管理
     * @return 物料管理
     */
    @Override
    public List<SinterMaterials> selectSinterMaterialsList(SinterMaterials sinterMaterials) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(sinterMaterials)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterMaterials.getMaterialCode()), "material_code", sinterMaterials.getMaterialCode());
            queryWrapper.like(ObjectUtil.isNotEmpty(sinterMaterials.getMaterialName()), "material_name", sinterMaterials.getMaterialName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterMaterials.getMaterialCategory()), "material_category", sinterMaterials.getMaterialCategory());
        }
        return sinterMaterialsMapper.selectList(queryWrapper);
    }

    /**
     * 新增物料管理
     *
     * @param sinterMaterials 物料管理
     * @return 结果
     */
    @Override
    public int insertSinterMaterials(SinterMaterials sinterMaterials) {
        sinterMaterials.setCreateBy(SecurityUtils.getUsername());
        sinterMaterials.setCreateDt(new Date());
        return sinterMaterialsMapper.insert(sinterMaterials);
    }

    /**
     * 修改物料管理
     *
     * @param sinterMaterials 物料管理
     * @return 结果
     */
    @Override
    public int updateSinterMaterials(SinterMaterials sinterMaterials) {
        sinterMaterials.setUpdateBy(SecurityUtils.getUsername());
        sinterMaterials.setUpdateDt(new Date());
        return sinterMaterialsMapper.updateById(sinterMaterials);
    }

    /**
     * 删除物料管理
     *
     * @param id 物料管理ID
     * @return 结果
     */
    @Override
    public int deleteSinterMaterialsById(Long id) {
        return sinterMaterialsMapper.deleteById(id);
    }

    /**
     * 批量删除物料管理
     *
     * @param ids 物料管理ID
     * @return 结果
     */
    @Override
    public int deleteSinterMaterialsByIds(Long[] ids) {
        if (ids.length == 0) return 0;
        return sinterMaterialsMapper.deleteBatchIds(ListUtil.toList(ids));
    }


    @Override
    public SinterMaterials selectSinterMaterialsByCode(SinterMaterials sinterMaterials) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(sinterMaterials)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterMaterials.getMaterialCode()), "material_code", sinterMaterials.getMaterialCode());
            queryWrapper.like(ObjectUtil.isNotEmpty(sinterMaterials.getMaterialName()), "material_name", sinterMaterials.getMaterialName());
            queryWrapper.last("limit 1");
        }
        return sinterMaterialsMapper.selectOne(queryWrapper);
    }
}
